cmake_minimum_required(VERSION 3.10)
project(KWDB_SDK)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# 设置编译选项
if(MSVC)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W3")
else()
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -O2")
endif()

# 包含目录
include_directories(include)
include_directories(${CMAKE_CURRENT_BINARY_DIR})  # 用于包含生成的proto文件
include_directories(/snap/protobuf/current/include)  # snap安装的protobuf
include_directories(third_party/grpc/include)
include_directories(third_party/protobuf/include)

# 查找依赖库
find_package(Threads REQUIRED)
find_package(PkgConfig REQUIRED)

# 查找gRPC
pkg_check_modules(GRPC REQUIRED grpc++)
pkg_check_modules(PROTOBUF REQUIRED protobuf)

# 如果找不到gRPC，使用本地构建
if(NOT GRPC_FOUND)
    message(STATUS "使用本地gRPC构建")
    add_subdirectory(third_party/grpc)
    set(GRPC_LIBRARIES grpc++)
    set(PROTOBUF_LIBRARIES protobuf)
endif()

# 源文件
set(SOURCES
    src/kwdb_client.cpp
    src/data_point.cpp
    src/batch_writer.cpp
    src/grpc_client.cpp
    src/line_protocol_parser.cpp
    src/query_builder.cpp
    src/query_result.cpp
    src/exceptions.cpp
)

# 头文件
set(HEADERS
    include/kwdb_sdk.h
    include/kwdb_client.h
    include/data_point.h
    include/batch_writer.h
    include/grpc_client.h
    include/line_protocol_parser.h
    include/query_builder.h
    include/query_result.h
    include/config.h
    include/exceptions.h
)

# 创建静态库
add_library(kwdb_sdk_static STATIC ${SOURCES} ${HEADERS})
target_link_libraries(kwdb_sdk_static 
    Threads::Threads
    ${GRPC_LIBRARIES}
    ${PROTOBUF_LIBRARIES}
)

# 创建动态库
add_library(kwdb_sdk_shared SHARED ${SOURCES} ${HEADERS})
target_link_libraries(kwdb_sdk_shared 
    Threads::Threads
    ${GRPC_LIBRARIES}
    ${PROTOBUF_LIBRARIES}
)

# 设置输出目录
set_target_properties(kwdb_sdk_static PROPERTIES
    ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib
)

set_target_properties(kwdb_sdk_shared PROPERTIES
    LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib
    RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin
)

# 安装规则
install(TARGETS kwdb_sdk_static kwdb_sdk_shared
    ARCHIVE DESTINATION lib
    LIBRARY DESTINATION lib
    RUNTIME DESTINATION bin
)

install(FILES ${HEADERS} DESTINATION include)

# 示例程序
add_executable(basic_example examples/basic_example.cpp)
target_link_libraries(basic_example kwdb_sdk_static)

add_executable(performance_example examples/performance_example.cpp)
target_link_libraries(performance_example kwdb_sdk_static)

# 编译选项
option(BUILD_TESTS "Build tests" OFF)
option(BUILD_EXAMPLES "Build examples" ON)

# 生成gRPC代码
set(PROTO_FILES
    ${CMAKE_CURRENT_SOURCE_DIR}/../proto/kwdb.proto
)

# 创建proto输出目录
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/proto)

foreach(PROTO_FILE ${PROTO_FILES})
    get_filename_component(PROTO_NAME ${PROTO_FILE} NAME_WE)
    set(PROTO_SRCS "${CMAKE_CURRENT_BINARY_DIR}/proto/${PROTO_NAME}.pb.cc")
    set(PROTO_HDRS "${CMAKE_CURRENT_BINARY_DIR}/proto/${PROTO_NAME}.pb.h")
    set(GRPC_SRCS "${CMAKE_CURRENT_BINARY_DIR}/proto/${PROTO_NAME}.grpc.pb.cc")
    set(GRPC_HDRS "${CMAKE_CURRENT_BINARY_DIR}/proto/${PROTO_NAME}.grpc.pb.h")
    
    add_custom_command(
        OUTPUT "${PROTO_SRCS}" "${PROTO_HDRS}" "${GRPC_SRCS}" "${GRPC_HDRS}"
        COMMAND protoc
        ARGS --cpp_out=${CMAKE_CURRENT_BINARY_DIR}
             --grpc_out=${CMAKE_CURRENT_BINARY_DIR}
             --plugin=protoc-gen-grpc=/usr/bin/grpc_cpp_plugin
             -I ${CMAKE_CURRENT_SOURCE_DIR}/..
             ${PROTO_FILE}
        DEPENDS ${PROTO_FILE}
        COMMENT "Generating protobuf and gRPC C++ files from ${PROTO_FILE}"
    )
    
    list(APPEND PROTOBUF_GENERATED_SRCS ${PROTO_SRCS} ${GRPC_SRCS})
    list(APPEND PROTOBUF_GENERATED_HDRS ${PROTO_HDRS} ${GRPC_HDRS})
endforeach()

# 添加生成的源文件到库
target_sources(kwdb_sdk_static PRIVATE ${PROTOBUF_GENERATED_SRCS})
target_sources(kwdb_sdk_shared PRIVATE ${PROTOBUF_GENERATED_SRCS})
